package com.liangsui.mss.controller.system;

import com.liangsui.mss.aspectj.Log;
import com.liangsui.mss.aspectj.enums.BusinessType;
import com.liangsui.mss.dto.UserDto;
import com.liangsui.mss.service.UserService;
import com.liangsui.mss.utils.ShiroSecurityUtils;
import com.liangsui.mss.vo.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;

/**
 * UserController
 *
 * @author zhangjiawei
 * @email 1533167396@qq.com
 * @date 2022/12/5 16:49
 */
@RestController
@RequestMapping("system/user")
public class UserController {
    @Autowired
    private UserService userService;

    /**
     * @功能描述: 用户分页查询
     * @Author: TeouBle
     * @Date: 2020/9/8 0：0：0
     */
    @GetMapping("listUserForPage")
    public AjaxResult listUserForPage(UserDto userDto) {
        return AjaxResult.success(userService.listUserForPage(userDto));
    }

    /**
     * @功能描述:查询所有可用的用户
     * @Author: TeouBle
     * @Date: 2020/9/8 0：0：0
     */
    @GetMapping("selectAllUser")
    public AjaxResult selectAllUser() {
        return AjaxResult.success(this.userService.getAllUsers());
    }

    /**
     * @功能描述: 用户添加
     * @Author: TeouBle
     * @Date: 2020/9/8 0：0：0
     */
    @PostMapping("addUser")
    @Log(title = "用户添加", businessType = BusinessType.INSERT)
    public AjaxResult addUser(@Validated UserDto userDto) {
        // 设置添加人员
        userDto.setSimpleUser(ShiroSecurityUtils.getCurrentSimpleUser());
        return AjaxResult.toAjax(this.userService.addUser(userDto));
    }

    /**
     * @功能描述: 用户修改
     * @Author: TeouBle
     * @Date: 2020/9/8 0：0：0
     */
    @PutMapping("updateUser")
    @Log(title = "用户修改", businessType = BusinessType.UPDATE)
    public AjaxResult updateUser(@Validated UserDto userDto) {
        try {
            // 设置修改人员
            userDto.setSimpleUser(ShiroSecurityUtils.getCurrentSimpleUser());
            return AjaxResult.toAjax(this.userService.updateUser(userDto));
        } catch (Exception e) {
            return AjaxResult.ex(e.getMessage());
        }
    }

    /**
     * @功能描述: 根据ID查询一个用户信息
     * @Author: TeouBle
     * @Date: 2020/9/8 0：0：0
     */
    @GetMapping("getUserById/{userId}")
    @Log(title = "根据ID查询一个用户信息", businessType = BusinessType.SELECT)
    public AjaxResult getUserByID(@NotNull(message = "用户ID不能为空")
                                      @PathVariable @Validated Long userId) {
        try {
            return AjaxResult.success(this.userService.getUserById(userId));
        } catch (Exception e) {
            return AjaxResult.ex(e.getMessage());
        }
    }

    /**
     * @功能描述:删除
     * @Author: TeouBle
     * @Date: 2020/9/8 0：0：0
     */
    @DeleteMapping("deleteUserByIds/{userIds}")
    @Log(title = "根据ID删除用户", businessType = BusinessType.DELETE)
    public AjaxResult updateUser(@PathVariable @Validated
                                     @NotEmpty(message = "要删除的用户ID不能为空") Long[] userIds) {
        try {
            return AjaxResult.toAjax(this.userService.deleteUserByIds(userIds));
        } catch (Exception e) {
            return AjaxResult.ex(e.getMessage());
        }
    }

    /**
     * 重置密码
     */
    @PostMapping("resetPwd/{userIds}")
    public AjaxResult resetPwd(@PathVariable Long[] userIds) {
        if (userIds.length > 0) {
            this.userService.resetPassWord(userIds);
            return AjaxResult.success("重置成功");
        }
        return AjaxResult.fail("重置失败,没有选择用户");

    }
}
